/**
 * waterQuality vuex
 */
import posts from '@/plugins/request'
import common from '@/plugins/common'
import { Notification } from 'element-ui'
// initial state
const state = {
  quaEvalist: {
    qualityData: [],
    analysisPieData: [],
    pollFactorData: [],
    companyTypeTable: [],
    qualityExceedNum: [],
    hasCod: 0
  },
  quaEvaLine: [],
  qualityContrast: {
    line: [],
    table: []
  },
  qualityRank: {
    factorRank: {},
    rankAnalysis: [],
  }
}
// getters
const getters = {
  getQualityData: state => state.quaEvalist.qualityData,
  getAnalysisPieData: state => state.quaEvalist.analysisPieData,
  getPollFactorData: state => state.quaEvalist.pollFactorData,
  getCompanyTypeTable: state => state.quaEvalist.companyTypeTable,
  getQualityExceedNum: state => state.quaEvalist.qualityExceedNum,
  getQualityHasCod: state => state.quaEvalist.hasCod,
  getQuaEvaLine: state => state.quaEvaLine,
  getQualityContrastLine: state => state.qualityContrast.line,
  getQualityContrastTable: state => state.qualityContrast.table,

  getQualityFactorRankChart: state => state.qualityRank.factorRank,
  getRankAnalysisChart: state => state.qualityRank.rankAnalysis,
}
// actions

const actions = {
  async fetchQualityEvaluationChart({ commit }, param) {
    let data = await posts.postQualityEvaluationChart(param)
    let qualityData = []
    for(let i in data.data) {
      qualityData.push({
        xAxis: i,
        value: data.data[i],
      })
    }
    
    let exceedNum = []
    for(let i in data.countMap) {
      exceedNum.push({
        xAxis: i,
        value: data.countMap[i],
      })
    }
    commit('changeQualityData', qualityData)
    commit('changeQualityExceedNum', exceedNum)
  },
  async fetchQualityEvaluationLine({ commit }, param) {
    let data = await posts.postQualityEvaluationLine(param)
    commit('changeQuaEvaLine', data)
  },
  async fetchQualityEvaDetailChart({ commit }, param) {
    let data = await posts.postQualityEvaDetailChart(param)
    if(data.remind) {
      return data.remind
    }
    let piedata = []
    let hasNoData = []
    for(let i = 0; i < data.data1.length; i++) { 
      for(let j in data.data1[i]) {
        if(data.data1[i][j] === -1) {
          hasNoData.push(j)
        }
        piedata.push({
          enName: j,
          name: common.translate(j),
          num1: data.data1[i][j] === -1 ? null : (data.data1[i][j] * 100).toFixed(2), //超标率
          num2: data.data1[i][j] === -1 ? null : (100 - data.data1[i][j] * 100).toFixed(2), //达标率
        })
      }
    }
    if(hasNoData.length) {
      let str = ""
      hasNoData.map(v => str += (common.translate(v) + " "))
      Notification.warning({
        title: '提示',
        message: str + "，在当前时间段没有有效数据！",
        duration: 0
      })
    }
    let companyTypeTable = []
    for(let key in data.data3) {
      companyTypeTable.push({
        name: key,
        sourceType: data.data3[key],
      })
    }

    commit('changeAnalysisPieData', piedata)
    commit('changePollFactorData', data.data2)
    commit('changeCompanyTypeTable', companyTypeTable)
    commit("changeQuaEvalistHasCod", data.hasCod)
  },
  async fetchQualityContrast({ commit }, param) {
    let data = await posts.postQualityContrast(param)
    let keyFactors = data.keyFactors || []
    let pollution = data.pollution || []
    if(keyFactors.length) {
      for(let i = 0;i < keyFactors.length; i++) {
        keyFactors[i].improveFactor = common.translate(keyFactors[i].improveFactor)
        keyFactors[i].increaseFactor = common.translate(keyFactors[i].increaseFactor)
        keyFactors[i].stableFactor = common.translate(keyFactors[i].stableFactor)
        keyFactors[i].overFactor = common.translate(keyFactors[i].overFactor)
      }
    }
    let pollutionResult = []
    if(pollution.length) {
      for(let j = 0; j < pollution.length; j++) {
        if(pollution[j].type === '0') {
          //时间大于一天
          pollutionResult.push({
            result: pollution[j].result,
            time: pollution[j].time,
            type: pollution[j].type
          })
        }else {
          //时间小于一天
          pollutionResult.push({
            result: pollution[j].result,
            time: pollution[j].period,
            type: pollution[j].type
          })
        }
      }
    }
    commit('changeQualityContrastLine', pollutionResult)
    commit('changeQualityContrastTable', keyFactors)
    if(data.message === "fail") {
      return data.info
    }
  },
  async fetchFactorChart({ commit }, param) {
    let factorChart = await posts.postFactorRankChart(param)
    commit('changeFactorChart', factorChart)
  },
  async fetchAnalysisChart({ commit }, param) {
    let analysisChart = await posts.postRankAnalysisChart(param)
    commit('changeRankAnalysisChart', analysisChart)
  },
  async postQualityEvaDetailList(payload, param) {
    const res = await posts.postQualityEvaDetailList(param)
    return res
  },

}

// mutations
const mutations = {
  changeQualityData(state, data) {
    state.quaEvalist.qualityData = data
  },
  changeAnalysisPieData(state, data) {
    state.quaEvalist.analysisPieData = data
  },
  changePollFactorData(state, data) {
    state.quaEvalist.pollFactorData = data
  },
  changeCompanyTypeTable(state, data) {
    state.quaEvalist.companyTypeTable = data
  },
  changeQualityExceedNum(state, data) {
    state.quaEvalist.qualityExceedNum = data
  },
  changeQuaEvaLine(state, data) {
    state.quaEvaLine = data
  },
  changeQualityContrastLine(state, data) {
    state.qualityContrast.line = data
  },
  changeQualityContrastTable(state, data) {
    state.qualityContrast.table = data
  },
  changeFactorChart(state, data) {
    state.qualityRank.factorRank = data
  },
  changeRankAnalysisChart(state, data) {
    state.qualityRank.rankAnalysis = data
  },
  changeQuaEvalistHasCod(state, data) {
    state.quaEvalist.hasCod = data
  },
}


export default {
  state,
  getters,
  actions,
  mutations
}